var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({
  data:{},
  onLoad:function(options){
      var that = this;  
        var goods = new Array();        
        
        var key = "";
        key = options.key;  
       
        if (!key) {          
          if (wx.getStorageSync("key")){
            key = wx.getStorageSync("key");  
          }else{ 
            wx.reLaunch({
              url: '/pages/index/index',
            })            
            return false;
          }       
        } 
        var sum = '';
        var cart_list = {};
        var cart_count = '';
        var rData ={};
        var buyNum = new Array;
          wx.request({
              url: ApiUrl + "/index.php?act=member_cart&op=cart_list",
              data: {'key':key},
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: "POST",
              success:function(res){
                  cart_list  = res.data.datas.cart_list;
                  var cart_array = new Array;
                  var cart_listArr = new Array;
                  var o=0;              
                  sum        = res.data.datas.sum;
                  cart_count = res.data.datas.cart_count;
                  var o = 0;
                  for(var i=0;i<cart_list.length;i++){
                    for (var g = 0; g < cart_list[i]['goods']['length'];g++){
                      cart_array[o] = cart_list[i]['goods'][g]['cart_id'] + '|' + cart_list[i]['goods'][g]['goods_num'];
                      buyNum[cart_list[i]['goods'][g]['cart_id']] = cart_list[i]['goods'][g]['goods_num'];
                      o++;
                    } 
                  }         
                  cart_listArr = cart_array.toString();  
                
                  rData = { cart_list:cart_list, sum:sum, cart_count:cart_count,check_out: true };
                  wx.setStorageSync('cart_count',cart_count);
                  wx.setStorageSync("cart_listArr", cart_listArr);
                  wx.setStorageSync('cart_list', cart_listArr)
                  
                  that.setData({
                    cart_listArr: cart_listArr,
                    rData: rData,
                    cart_list:cart_list,
                    buyNum: buyNum,
                    sum: sum,   
                    isok:'ok',
                    key: key                    
                  });
              }
          })
      
  },
  /*随便逛逛*/
  redicrets:function(){
    wx.reLaunch({
      url: '/pages/index/index',      
    })     
    
  },
  goodsDetail:function( val ){
   
    wx.reLaunch({
      url: '/pages/product_detail/product_detail?goods_id=' + val.currentTarget.dataset.goods_id
    })
  },
 
  //删除购物车
  goodsDel:function (event) {
      var that = this;
      var cart_count = wx.getStorageSync('cart_count');
      var sum = that.data.sum;
      var goodsPrice = event.currentTarget.dataset.goodsprice; 
      var num = event.currentTarget.dataset.num; 
      var key = event.currentTarget.dataset.key;    
   
      wx.showModal({
        content: "确认删除吗？",
        confirmText: "确定",
        cancelText: "取消",
        cancelColor: '#0080FF',
        confirmColor: '#0080FF',
        success: function (res) {         
            if (res.confirm) {
              
              var cart_id = event.currentTarget.dataset.cart_id;
           
                if(key){
                    wx.request({                    
                      url: ApiUrl + "/index.php?act=member_cart&op=cart_del",
                      data: {'key':key,'cart_id':cart_id},
                      header: {
                        'content-type':'application/x-www-form-urlencoded'
                      },
                      method:"post",
                      success:function(result){
                          wx.redirectTo({
                            url: '/pages/carts_list/carts_list?key='+key,
                          })  
                      }
                    })
                }   
          }
        },           
           
    });   
  },
  listenCheckboxChange:function(event){
  
      var cart = new Array;
      cart = event.detail.value;
      
      var cart_list = "";
      var ok = '';
      var price = 0;    
      for(var i=0;i<cart.length;i++){
        
          var priceList = cart[i].split('|');
          price += parseFloat(priceList[1]) * parseFloat(priceList[2]);
          
          var n = cart[i].lastIndexOf('|');
          var cart1 = cart[i].substring(0,n);
          
          if(cart_list){
            cart_list += ',' + cart1; 
          }else{
            cart_list = cart1;
          }
      } 
      
      var cart_listArr =cart_list;
      wx.setStorageSync("cart_listArr", cart_listArr);
      if (cart_listArr){
        ok = 'ok';
      }    
      
      this.setData({
        cart_listArr: cart_listArr,
        isok:ok,
        sum: price
      });

  },

  editQuantity:function(event){
     
      var that = this;   

      var buytype = event.currentTarget.dataset.buytype;
      var cart_id = event.currentTarget.dataset.cart_id;
      var buynum = event.currentTarget.dataset.num;
      var goods_id = event.currentTarget.dataset.goods_id;
      var goodsPrice = event.currentTarget.dataset.goodsprice; 
      var quantity = 1;
      var newQuantity = that.data.buyNum;
      var sum = that.data.sum;
      var key = event.currentTarget.dataset.key;
      if (buytype == "add") {
        quantity = parseInt(buynum) + 1;
        sum = parseFloat(sum) + parseFloat(goodsPrice);
      } else {
        if (buynum > 1) {
          quantity = parseInt(buynum) - 1;
          sum = parseFloat(sum) - parseFloat(goodsPrice);
        } else {
          return false;
        }
      }      
      wx.request({
        url: ApiUrl + "/index.php?act=member_cart&op=cart_edit_quantity",
        data: { 'key': key, 'cart_id': cart_id, 'quantity': quantity },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: "post",
        success: function (res) {          
           newQuantity[cart_id] = res.data.datas.quantity;          
            that.setData({
              buyNum: newQuantity,
              sum: sum.toFixed(2)              
            });         
        }
      })     
}

})

    